package com.ysalliance.getFan.organization.ui.task;

import android.content.Intent;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.google.gson.Gson;
import com.ysalliance.getFan.organization.Bean.ClassBean;
import com.ysalliance.getFan.organization.Bean.SearchVideoBean;
import com.ysalliance.getFan.organization.Bean.TaskBean;
import com.ysalliance.getFan.organization.Bean.bus.TaskUpdateIsNeedFresh;
import com.ysalliance.getFan.organization.LucasUtil;
import com.ysalliance.getFan.organization.R;
import com.ysalliance.getFan.organization.myview.ItemClassView;
import com.ysalliance.getFan.organization.network.Network;
import com.ysalliance.getFan.organization.ui.EditTextOneActivity;
import com.ysalliance.getFan.organization.ui.SearchVideoActivity;
import com.ysalliance.getFan.organization.ui.base.LucasBaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import androidx.annotation.NonNull;
import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

import static com.ysalliance.getFan.organization.FanerConstant.SET_TASK_NAME;


/**
 * 编辑任务
 * @author lucas
 * created at 2020-02-24 14:54
 */
public class UpdateTaskActivity extends LucasBaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_done)
    TextView tv_done;

    @BindView(R.id.item_task_name)
    ItemClassView item_task_name;     //任务名称
    @BindView(R.id.item_related_course)
    ItemClassView item_related_course;   //配套课程

    private SearchVideoBean searchVideoBean;

    private TaskBean taskBean;

    @Override
    public int bindContentView() {
        return R.layout.activity_add_task;
    }

    @Override
    public void afterView() {
        super.afterView();


        taskBean = (TaskBean)getIntent().getSerializableExtra("task");
        tv_title.setText("编辑任务");
        item_task_name.setRightText(taskBean.getName());

        getTask(taskBean.getId());

        initClick();


    }


    public void initClick() {
        //任务名称
        item_task_name.setOnClickListener(v->{

            Intent intent = new Intent(Utils.getApp(), UpdateTaskEditActivity.class);
            intent.putExtra("task", taskBean);
            ActivityUtils.startActivityForResult(mContext, intent,1);


        });

        //配套课程
        item_related_course.setOnClickListener(v->{

            Intent intent = new Intent(Utils.getApp(), SearchVideoActivity.class);
            ActivityUtils.startActivityForResult(mContext, intent,2);

        });

        //完成
        tv_done.setOnClickListener(v->{
            update_task(taskBean.getId(), taskBean.getName(), taskBean.getVideo().getId());
        });



    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK){
                String task_name = data.getExtras().getString("task_name");
                item_task_name.setRightText(task_name);
                taskBean.setName(task_name);
            }
        }
        if (requestCode == 2) {
            if (resultCode == RESULT_OK){
                searchVideoBean = (SearchVideoBean)data.getSerializableExtra("slected_video");
                item_related_course.setRightText("《"+searchVideoBean.getName() + "》 " + searchVideoBean.getAria());
                taskBean.setVideo(searchVideoBean);
            }
        }
    }




    /**  修改任务   */
    public void update_task(String task_id, String name, String video){

        disposable = Network.getUtilApi()
                .update_task(task_id, name, video)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json(" 修改任务  的返回", res);

                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){
                            ToastUtils.showShort("修改成功");
                            EventBus.getDefault().postSticky(new TaskUpdateIsNeedFresh(true));
                            finish();

                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    /**  获取具体任务   */
    public void getTask(String task_id){

        disposable = Network.getUtilApi()
                .getTask(task_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json(" 获取具体任务 的返回", res);

                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){
                            taskBean = new Gson().fromJson(jsonObject.optString("data"), TaskBean.class);
                            item_related_course.setRightText(taskBean.getVideo().getAria());
                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }


}
